#coding:utf-8
from flask import render_template, request, current_app, redirect,\
    url_for, flash, make_response
from . import admin
from ..models import User, HomePageInfo
from .. import db
from .forms import FirstPageForm, FirstPageFavorForm, FirstPageWorkForm, FirstPageCommentForm, FirstPageLordForm, FirstPageCommanderForm, FirstPageFooterForm
import os
import re
import json

 
@admin.route('/')
def index():
    return redirect('admin/testbase')

@admin.route('/infomation')
def infomation():
    return render_template('admin/infomation.html')

@admin.route('/upload')
def upload():
    return render_template('admin/ph.html')

@admin.route('/update_first_page_info')
def update_first_page_info():
    pass

@admin.route('/testbase', methods=['GET', 'POST'])
def testbase():
    form = FirstPageForm()
    if form.validate_on_submit():
        #name
        old_name = HomePageInfo.query.filter_by(type='name').first()
        if old_name.value == form.name.data:
            pass
        else:
            old_name.value = form.name.data
            db.session.add(old_name)
            db.session.commit()
        #first_one
        old_first_one = HomePageInfo.query.filter_by(type='firstOne').first()
        if old_first_one.value == form.firstone.data:
            pass
        else:
            old_first_one.value = form.firstone.data
            db.session.add(old_first_one)
            db.session.commit()
        #firstTwo
        old_first_two = HomePageInfo.query.filter_by(type='firstTwo').first()
        if old_first_two.value == form.firsttwo.data:
            pass
        else:
            old_first_two.value = form.firsttwo.data
            db.session.add(old_first_two)
            db.session.commit()
        return render_template('admin/base.html', form=form)
    if form.errors:
        pass

    formfavor = FirstPageFavorForm()
    formwork = FirstPageWorkForm()
    formcomment = FirstPageCommentForm()
    formlord = FirstPageLordForm()
    formcommander = FirstPageCommanderForm()
    formfooter = FirstPageFooterForm()    
    return render_template('admin/base.html', form=form, formfavor=formfavor, formwork=formwork, formcomment=formcomment, formlord=formlord, formcommander=formcommander, formfooter=formfooter)

@admin.route('/submit-articles', methods=['GET', 'POST'])
def submitArticles():
    form = SubmitArticlesForm()

    sources = [(s.id, s.name) for s in Source.query.all()]
    form.source.choices = sources
    types = [(t.id, t.name) for t in ArticleType.query.all()]
    form.types.choices = types

    if form.validate_on_submit():
        title = form.title.data
        source_id = form.source.data
        content = form.content.data
        type_id = form.types.data
        summary = form.summary.data

        source = Source.query.get(source_id)
        articleType = ArticleType.query.get(type_id)

        if source and articleType:
            article = Article(title=title, content=content, summary=summary,
                              source=source, articleType=articleType)
            db.session.add(article)
            db.session.commit()
            flash(u'发表博文成功！', 'success')
            article_id = Article.query.filter_by(title=title).first().id
            return redirect(url_for('main.articleDetails', id=article_id))
    if form.errors:
        flash(u'发表博文失败', 'danger')

    return render_template('admin/submit_articles.html', form=form)

